home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / uupc11ys.zip / LIB / ACTIVE.C next >
C/C++ Source or Header  |  1993-04-12  |  9KB  |  287 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    a c t i v e . c                                                 */
  3. /*                                                                    */
  4. /*    Load and write UUPC/extended news active file                   */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*
  8.  *    $Id: active.c 1.4 1993/04/11 00:31:31 dmwatt Exp $
  9.  *
  10.  *    $Log: active.c $
  11.  * Revision 1.4  1993/04/11  00:31:31  dmwatt
  12.  * Global edits for year, TEXT, etc.
  13.  *
  14.  * Revision 1.3  1993/03/06  22:48:23  ahd
  15.  * Correct header files
  16.  *
  17.  * Revision 1.2  1992/11/23  03:56:06  ahd
  18.  * Use strpool for news group names
  19.  *
  20.  */
  21.  
  22. /*
  23.    This file contains routines that muck with the "active" file.
  24.  
  25.    The file is named "active" and is in the configuration directory
  26.    defined in UUPC.RC.
  27.  
  28.    The file is a direct copy of the UNIX active file which is
  29.    defined and described in the Nutshell book "Managing UUCP
  30.    and Usenet".
  31.  
  32.    IMPORTANT:
  33.    ----------
  34.       Almost no checking is performed on the contents of the file.
  35.       It is critically important that the system administrator
  36.       maintain the file carefully.
  37.  
  38.    The file consists of one line for each newsgroup to be received.
  39.    Articles destined for newsgroups which do not have a line in the
  40.    active file are lost.
  41.  
  42.    Each line consists of four fields separated by spaces.
  43.  
  44.    The fields are:
  45.  
  46.       group high low post
  47.  
  48.    where:
  49.       group the newsgroup name.  Case is important.  E.g., ba.food
  50.             This field is maintained by the administrator.
  51.             This field must be less than 50 characters long.
  52.  
  53.       high  highest article number in the group.  (Zero for a
  54.             new group.)  This field is maintained by rnews.
  55.  
  56.       low   lowest article number in the group.  (Zero for a
  57.             new group.)  This field is maintained by expire.
  58.  
  59.       post  can the user post to this newsgroup.  A single
  60.             character. 'y' for yes; 'n' for no; 'm'
  61.             for moderated.  This field is maintained
  62.             by the administrator.
  63.  
  64. */
  65.  
  66. #include <stdio.h>
  67.  
  68. #ifndef __GNUC__
  69. #include <io.h>
  70. #include <conio.h>
  71. #endif
  72.  
  73. #include <stdlib.h>
  74. #include <string.h>
  75. #include <ctype.h>
  76. #include <time.h>
  77. #include <sys/types.h>
  78. #include <sys/stat.h>
  79.  
  80. #include "lib.h"
  81. #include "hlib.h"
  82. #include "timestmp.h"
  83. #include "active.h"
  84. #include "importng.h"
  85.  
  86. #include "getopt.h"
  87.  
  88. currentfile();
  89.  
  90. static boolean fallback = FALSE;
  91.  
  92. struct grp *group_list = NULL;      /* List of all groups */
  93.  
  94. /*--------------------------------------------------------------------*/
  95. /*    g e t _ a c t i v e                                             */
  96. /*                                                                    */
  97. /*    This function opens <newsdir>/active and extracts all the       */
  98. /*    information about the newsgroup we currently maintain           */
  99. /*--------------------------------------------------------------------*/
  100.  
  101. void get_active( void )
  102. {
  103.    char active_filename[FILENAME_MAX];
  104.    char grp_name_tmp[51];     /* Space to hold the group field being read in */
  105.    FILE *g;
  106.    struct grp *cur_grp;
  107.    struct grp *prev_grp;
  108.    int i;
  109.  
  110. /*--------------------------------------------------------------------*/
  111. /*    Open the active file and extract all the newsgroups and         */
  112. /*    their next number.                                              */
  113. /*--------------------------------------------------------------------*/
  114.  
  115. /*--------------------------------------------------------------------*/
  116. /*     Try configuration directory first, then try news directory     */
  117. /*--------------------------------------------------------------------*/
  118.  
  119.    mkfilename(active_filename, E_confdir, ACTIVE);
  120.    g = FOPEN(active_filename,"r",TEXT_MODE);
  121.  
  122.    if (g == NULL)
  123.    {
  124.       printerr(active_filename);
  125.  
  126.       mkfilename(active_filename, E_newsdir, ACTIVE);
  127.       fallback= TRUE;
  128.       g = FOPEN(active_filename,"r",TEXT_MODE);
  129.    } /* if */
  130.  
  131.    if (g == NULL) {
  132.       printerr(active_filename);
  133.       panic();
  134.    }
  135.  
  136.    prev_grp = NULL;
  137.  
  138.    group_list = (struct grp *) malloc(sizeof(struct grp));
  139.    cur_grp = group_list;
  140.  
  141.    cur_grp->grp_next = NULL;
  142.    cur_grp->grp_name = NULL;
  143.    cur_grp->grp_low  = 0;
  144.    cur_grp->grp_high = 0;
  145.    cur_grp->grp_can_post = ' ';
  146.  
  147.    while ((i = fscanf(g, "%s %ld %ld %1s\n", &grp_name_tmp[0],
  148.             &cur_grp->grp_high,
  149.             &cur_grp->grp_low,
  150.             &cur_grp->grp_can_post)) != EOF)
  151.    {
  152.       if (i != 4)
  153.       {
  154.          printmsg(0,"rnews: incomplete line in %s, %d tokens found",
  155.                      active_filename, i);
  156.          panic();
  157.       }
  158.  
  159.       cur_grp->grp_name = newstr(grp_name_tmp);
  160.  
  161.       cur_grp->grp_high++;    /* It is stored as one less than we want it */
  162.  
  163.       prev_grp = cur_grp;
  164.       cur_grp = (struct grp *) malloc(sizeof(struct grp));
  165.       checkref(cur_grp);
  166.       prev_grp->grp_next = cur_grp;
  167.  
  168.       cur_grp->grp_next = NULL;
  169.       cur_grp->grp_name = NULL;
  170.       cur_grp->grp_low  = 0;
  171.       cur_grp->grp_high = 0;
  172.       cur_grp->grp_can_post = ' ';
  173.  
  174.    } /* while */
  175.  
  176.    if (fclose(g))
  177.       printerr( active_filename );
  178.  
  179.    if (prev_grp != NULL) {
  180.       prev_grp->grp_next = NULL;
  181.       free(cur_grp);
  182.    }
  183.  
  184.    return;
  185. } /* get_active */
  186.  
  187. /*--------------------------------------------------------------------*/
  188. /*    p u t _ a c t i v e                                             */
  189. /*                                                                    */
  190. /*    Update current active file                                      */
  191. /*--------------------------------------------------------------------*/
  192.  
  193. void put_active()
  194. {
  195.    char active_filename[FILENAME_MAX];
  196.    FILE *g;
  197.    struct grp *cur_grp;
  198.  
  199.    mkfilename(active_filename, E_confdir, ACTIVE);
  200.  
  201.    filebkup( active_filename );
  202.  
  203.    g = FOPEN(active_filename,"w",TEXT_MODE);
  204.  
  205.    if (g == NULL) {
  206.       printmsg(0, "rnews: Cannot update active %s", active_filename );
  207.       printerr(active_filename);
  208.       panic();
  209.    }
  210.  
  211.    cur_grp = group_list;
  212.  
  213. /*--------------------------------------------------------------------*/
  214. /*           Loop to actually write out the updated groups            */
  215. /*--------------------------------------------------------------------*/
  216.  
  217.    while ((cur_grp != NULL) && (cur_grp->grp_name != NULL))
  218.    {
  219.       fprintf(g, "%s %ld %ld %c\n", cur_grp->grp_name,
  220.                               cur_grp->grp_high-1,
  221.                               cur_grp->grp_low,
  222.                               cur_grp->grp_can_post);
  223.       cur_grp = cur_grp->grp_next;
  224.    }
  225.  
  226.    fclose(g);
  227.  
  228. /*--------------------------------------------------------------------*/
  229. /*    Delete old (now obsolete) active file in the news directory     */
  230. /*--------------------------------------------------------------------*/
  231.  
  232.    if ( fallback )
  233.    {
  234.       mkfilename(active_filename, E_newsdir, ACTIVE);
  235.       filebkup( active_filename );
  236.    }
  237.  
  238. } /* put_active */
  239.  
  240. /*--------------------------------------------------------------------*/
  241. /*    v a l i d a t e _ n e w s g r o u p s                           */
  242. /*                                                                    */
  243. /*    Verify all the directories for news groups exist                */
  244. /*--------------------------------------------------------------------*/
  245.  
  246. void validate_newsgroups( void )
  247. {
  248.    char full_dirname[FILENAME_MAX];
  249.  
  250.    struct stat buff;
  251.  
  252.    struct grp *cur_grp;
  253.    int i;
  254.  
  255.    cur_grp = group_list;
  256.    while (cur_grp != NULL) {
  257.       ImportNewsGroup( full_dirname , cur_grp->grp_name, 0 );
  258.  
  259.       i = stat(full_dirname, &buff);
  260.       if (i != 0) {
  261.          /* Directory does not exist, create it */
  262.          printmsg(4,"Directory %s does not exist for group %s",
  263.                      full_dirname, cur_grp->grp_name );
  264.  
  265. #ifdef WASTE_SPACE
  266.          i = MKDIR(full_dirname);
  267.          if (i != 0) {
  268.             printf("Unable to create %s\n", full_dirname);
  269.             panic();
  270.          }
  271. #endif
  272.  
  273.       } else {
  274.          /* It exists.  Ensure that it is a directory */
  275.          if (!(buff.st_mode & S_IFDIR)) {
  276.             /* Yukk! */
  277.             printmsg(0,"validate_newsgroups: %s is a file not a directory",
  278.                    full_dirname);
  279.             panic();
  280.          }
  281.        }
  282.       cur_grp = cur_grp->grp_next;
  283.    }
  284.  
  285.    return;
  286. } /* validate_newsgroups */
  287.